import { createStore } from 'vuex'
// import  "../assets/css/loading.css";
import { ElLoading } from 'element-plus' // 配置全局加载动画
const keepLiveRoute = ['/home'] // 缓存页面数组
const store = createStore({
  state: {
    token: '', // 保存的token值
    userInfo: {}, // 保存的个人信息
    routerPath: '', // 保存的路由
    chartLsit: [], // 暂存本地的购物商品列表
    keepLiveRoute: '', // 缓存页面数组

    options: {
      lock: true,
      text: '加载中，请稍后....', //加载动画的文字
      spinner: 'el-icon-loading', //加载动画的图标
      background: 'rgb(255,255,255,1)', //加载动画的背景
    },
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
      localStorage.setItem('token', token)
    },
    SET_USER: (state, userInfo) => {
      state.userInfo = userInfo
      localStorage.setItem('userInfo', JSON.stringify(userInfo))
    },
    SET_Router_Path: (state, routerPath) => {
      state.routerPath = routerPath
      localStorage.setItem('routerPath', JSON.stringify(routerPath))
    },
    SET_cHART(state, chartLsit) {
      localStorage.setItem('chartLsit', JSON.stringify(chartLsit))
    },
    SHOW_LOADING(state, val) {
      let loadingInstance = ElLoading.service(state.options)
      val == true ? loadingInstance : loadingInstance.close() //这里判断调用方法时候的参数值，打开服务的时候传true，关闭服务的时候传false
    },
    ClearKeep(state) {
      if (state.keepLiveRoute.length > 0) state.keepLiveRoute = []
      console.log('清空缓存路由！')
    },
    SetKeep(state) {
      if (!state.keepLiveRoute.length > 0) state.keepLiveRoute = keepLiveRoute
      console.log('设置缓存路由！')
    },
  },
  getters: {
    getUserInfo(state) {
      state.userInfo = JSON.parse(window.localStorage.getItem('userInfo'))
      return state.userInfo
    },
    getRouterPath(state) {
      state.userInfo = JSON.parse(window.localStorage.getItem('routerPath'))
      return state.routerPath
    },
    getToken(state) {
      state.token = window.localStorage.getItem('token')
      return state.token
    },
    getChartList(state) {
      state.chartLsit = window.localStorage.getItem('chartLsit')
      return state.chartLsit
    },
  },
  actions: {},
})
export default store
